package com.kmvc.message.adapter;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Environment;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.BackgroundColorSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kmvc.message.activity.MessageApplication;
import com.kmvc.message.database.DbHelper;
import com.kmvc.message.R;

@SuppressLint("SimpleDateFormat")
public class GroupAdapter extends BaseAdapter  {

	private Context context;
	List<Map<String, Object>> list;
	String key;
	AlertDialog.Builder builder;
	AlertDialog al;
	MessageApplication application;
	ViewHolder holder;
	public class ViewHolder {
		public RelativeLayout rootlayout;
		TextView area;
		TextView time;
		TextView textcontent;
		TextView unreadmsg;
		ImageView icon;
	}

	public GroupAdapter(Context context, List<Map<String, Object>> list,
			String key) {
		this.context = context;
		this.list = list;
		this.key = key;

	}

	@Override
	public int getCount() {

		return this.list.size();
	}

	@Override
	public Object getItem(int position) {

		return this.list.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	@SuppressWarnings("deprecation")
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		 holder = null;
		if (convertView == null) 
		{
			LayoutInflater vi = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = vi.inflate(R.layout.recent_list_item, parent, false);
			holder = new ViewHolder();
			holder.rootlayout = (RelativeLayout) convertView
					.findViewById(R.id.relativeItem);
			holder.area = (TextView) convertView.findViewById(R.id.area);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.textcontent = (TextView) convertView
					.findViewById(R.id.textcontent);
			holder.unreadmsg=(TextView)convertView.findViewById(R.id.unreadmsg);
			holder.icon=(ImageView)convertView.findViewById(R.id.icon);
			
			
			if(holder.icon!=null)
			{
				holder.icon.setBackgroundResource(R.drawable.smil);
				if(this.list.get(position).get("IMG_URL2")!=null)
				{
					String SDPATH = Environment.getExternalStorageDirectory().getPath();
					String pathString=this.list.get(position).get("IMG_URL2").toString();
					String path=SDPATH+"/KMVC_MESSAGE/icon/"+pathString;
					File mfile=new File(path);
					if(mfile.exists())
					{
						Log.d("kmvc","文件存在！");
						try 
						{
							Bitmap bitmap=BitmapFactory.decodeFile(path);
							holder.icon.setBackgroundDrawable(new BitmapDrawable(bitmap));
						} 
						catch (Exception e) 
						{
							
						}
						
					}
					else 
					{
						Log.d("kmvc","文件存在！");
						DbHelper dbHelper=new DbHelper(context);
						dbHelper.DeleteSdImgState(pathString);
						dbHelper.close();
					}
				}
			}
			
			convertView.setTag(holder);
		} 
		else 
		{
			holder = (ViewHolder) convertView.getTag();
		}
		SpannableStringBuilder style = new SpannableStringBuilder(list
				.get(position).get("area").toString());
		try {
			int start = list.get(position).get("area").toString().indexOf(key);
			int end = start + key.length();
			if (start!=end) {
				style.setSpan(new BackgroundColorSpan(Color.parseColor("#FF8247")),
						start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			}
			
		} catch (Exception e) {

		}

		// holder.area.setText(list.get(position).get("area").toString());
		holder.area.setText(style);
		String dataString="";
		Date date=new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		SimpleDateFormat formatter1 = new SimpleDateFormat("HH:mm"); 
		try 
		{
			dataString=list.get(position).get("date").toString();
			formatter.parse(dataString);
			Date date2=new Date(System.currentTimeMillis());
			if(date2.getDate()==date.getDate())
			{
				dataString=formatter1.format(date);
			}
			else 
			{
				dataString=date.getMonth()+"/"+date.getDate();
			}
		} catch (Exception e) 
		{
			
		}

		
		
		holder.time.setText(dataString);
		
		String numNoRead="0";
		if(list.get(position).get("num")!=null)
		{
			numNoRead=list.get(position).get("num").toString();
		}
		
		if (!numNoRead.equals("0")) 
		{
			holder.unreadmsg.setBackgroundResource(R.drawable.skin_tab_newmessage2);
			holder.unreadmsg.setText(numNoRead+"");
		}
		else 
		{
			holder.unreadmsg.setBackgroundDrawable(null);
			holder.unreadmsg.setText("");
		}
		

		
		
		holder.textcontent
				.setText(this.list.get(position).get("content").toString());

		if (list.get(position).get("date").toString().contains(":")) {
			// String[] colors = new String[] { "#DFFFDF", "#F0FFF0" };
			//String[] colors = new String[] { "#FFE6D9", "#FFF3EE" };
			//int colorPos = position % colors.length;
			//holder.rootlayout.setBackgroundColor(Color.parseColor(colors[colorPos]));

		} else {

			// String[] colors = new String[] { "#FFE6D9", "#FFF3EE" };
			// int colorPos = position % colors.length;
			//holder.rootlayout.setBackgroundColor(Color.parseColor("#FFFFFF"));
		}
	 
		return convertView;
	}

	
	/**
	 * 添加列表项
	 * @param info
	 */
	public void addItem(Map<String, Object>  Object)
	{
		list.add(Object);
	}

	
}
